package com.caishi.lkx.exam.records.dto;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.LinkedHashMap;

public abstract class OssMapDto<I, T extends OssMapDto<I, T>> implements OssDto<T> {

    protected final JSONObject data = new JSONObject(new LinkedHashMap<>());


    @Override
    public T parse(String json) {
        if (StrUtil.isBlank(json)) return (T) this;
        data.putAll(JSON.parseObject(json));
        return (T) this;
    }

    @Override
    public String toJson() {
        return JSON.toJSONString(data);
    }

    public I put(String key, I value) {
        return (I) data.put(key, value);
    }

    public I remove(String key) {
        return (I) data.remove(key);
    }

    public I get(String key) {
        return data.getObject(key, itemClass());
    }

    public JSONObject getData() {
        return data;
    }

    public int size() {
        return data.size();
    }

    public boolean empty() {
        return data.isEmpty();
    }

    public abstract Class<I> itemClass();
}
